package yue.shen.game

import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import org.itheima.kotlin.game.core.Window
import yue.shen.business.AutoMoveAble
import yue.shen.business.BlockAble
import yue.shen.business.DestroyAble
import yue.shen.business.MoveAble
import yue.shen.enums.Direction
import yue.shen.model.*
import yue.shen.other.Config
import yue.shen.view.View
import java.io.File
import java.util.concurrent.CopyOnWriteArrayList

/**
 * Created by jiangzhuolei on 2017/11/7.
 */
class GameWindow : Window(title = "磊爷的坦克大战",width = Config.gameWidth,height = Config.gameHeight){

    //线程不安全的集合
    //private val views = arrayListOf<View>()
    //线程安全的集合
    private val views = CopyOnWriteArrayList<View>()

    private lateinit var tank:Tank

    override fun onCreate() {
        val file = File(javaClass.getResource("/map/1.map").path)
        val readLines:List<String> = file.readLines()
        var lineNum = 0
        readLines.forEach {
            var columnNum = 0
            it.toCharArray().forEach {
                when(it){
                    '砖'-> views.add(Wall(columnNum*Config.block,lineNum*Config.block))
                    '铁'-> views.add(Steel(columnNum*Config.block,lineNum*Config.block))
                    '草'-> views.add(Grass(columnNum*Config.block,lineNum*Config.block))
                    '水'-> views.add(Water(columnNum*Config.block,lineNum*Config.block))
                }
                columnNum++
            }
            lineNum++
        }

        tank = Tank(Config.block*10,Config.block*12)
        views.add(tank)
    }

    override fun onDisplay() {

        views.forEach {
            it.draw()
        }
    }

    override fun onKeyPressed(event: KeyEvent) {
        when(event.code){
            KeyCode.UP->tank.move(Direction.up)
            KeyCode.DOWN->tank.move(Direction.down)
            KeyCode.RIGHT->tank.move(Direction.right)
            KeyCode.LEFT->tank.move(Direction.left)
            KeyCode.A ->{
                val bullet = tank.shot()
                views.add(bullet)
            }
        }
    }

    override fun onRefresh() {

        views.filter { it is AutoMoveAble }.forEach {
            (it as AutoMoveAble).autoMove()
        }



        //找到运动物体
        views.filter { it is MoveAble }.forEach { move->
            //找到障碍物体
            move as MoveAble
            var badDirection:Direction? = null
            var badBlockAble:BlockAble? = null
            views.filter { it is BlockAble }.forEach blockTag@ { block->
                block as BlockAble
                //得到碰撞方向
                val direction = move.willCollision(block)

                direction?.let {
                    //发生碰撞
                    badDirection = direction
                    badBlockAble = block
                    return@blockTag
                }
            }
            move.notifyBlock(badDirection,badBlockAble)
        }

        //找到障碍
        views.filter { it is DestroyAble }.forEach {
            if((it as DestroyAble).isDestroyed()){
                views.remove(it)
            }
        }
    }
}